// import {BOMStructureBean, newEmptyInstanse} from '../bean/BOMStructureBean.js'
// import {StringUtils} from '../utils/StringUtils.js'
// import {ArrayUtils} from '../utils/ArrayUtils.js'
// import {PartUtils} from '../part/PartUtils.js'

import {BOMStructureBean, newEmptyInstanse} from 'ebom/bean/BOMStructureBean.js'
import {StringUtils} from 'ebom/utils/StringUtils.js'
import {ArrayUtils} from 'ebom/utils/ArrayUtils.js'
import {PartUtils} from 'ebom/part/PartUtils.js'

export const ImportStructureHelper = {

    parseImportFile: function (dataArray) {
        let parseInfo = this.parseBOMStructure(dataArray);

        let rootBean = parseInfo.rootBean;
        let structureBeans = parseInfo.structureBeans;
        this.createAllPart(structureBeans);

        this.buildPartUsageLink(rootBean);
    },

    createAllPart: function (structureBeans) {
        let numberArray = [];
        structureBeans.forEach(bean => {
            let number = bean.getNumber();
            if (!numberArray.includes(number)) {
                let partInfo = PartUtils.createPart(bean.getNumber(), bean.getName(), bean.getPhase());
                structureBeans
                    .filter(item => StringUtils.equals(item.getNumber(), number))
                    .forEach(item => item.setPartInfo(partInfo));
                numberArray.push(bean.getNumber());
            }
            let partInfo = bean.getPartInfo();
            let structureId = PartUtils.createPartStructure(partInfo.masterId, partInfo.iterationId);
            bean.setStructureId(structureId);
        })
    },

    parseBOMStructure: function (dataArray) {
        let structureBeans = [];
        let rootBean = newEmptyInstanse();
        for (let index = 0; index < dataArray.length; index++) {
            const element = dataArray[index];

            let level = element['序号'].toString();
            let number = element['图号'];

            let structureBean = new BOMStructureBean(
                index, level, number,
                element['名称'],
                element['单台数量'],
                element['零件类型'],
                element['零件类别'],
                element['阶段'],
                element['图纸版次']
            );

            structureBean.setNumberPath(number);

            if (level == '1') {
                rootBean = structureBean
            }
            structureBeans.push(structureBean);
        }
        if (rootBean) {
            this.travelEBOMStructure(structureBeans, rootBean)
        }
        return {
            rootBean: rootBean,
            structureBeans: structureBeans,
        };
    },

    buildPartUsageLink: function (parentBean) {
        console.log('parentBean:', parentBean);
        let children = parentBean.getChildren();
        let parentInfo = parentBean.getPartInfo();

        children.forEach(childBean => {
            console.log('childBean:', childBean);
            let childInfo = childBean.getPartInfo();
            PartUtils.createPartUsageLinkUpdateStructure(parentInfo.iterationId, childInfo.masterId, childBean.getQuantity(), childBean.getStructureId(), parentBean.getStructureId());
            this.buildPartUsageLink(childBean);
        });
    },

    travelEBOMStructure: function (structureBeans, structureBean) {
        this.findChildrenFromBeans(structureBeans, structureBean);
        let collect = structureBean.getChildren();

        if (collect.length > 0) {
            collect.forEach(item => {
                item.setNumberPath(structureBean.getNumberPath().concat('|', item.getNumber()));
                item.setParentNumber(structureBean.getNumber());
                this.travelEBOMStructure(structureBeans, item);
            });
        }
    },

    findChildrenFromBeans: function (structureBeans, structureBean) {
        let level = structureBean.getLevel() + ".";
        if (StringUtils.equals(structureBean.getLevel(), "0")) {

            let firstLevelStructureList = structureBeans
                .filter(item => !StringUtils.equals(item.getLevel(), "0"))
                .filter(item => !StringUtils.contains(item.getLevel(), "."));

            structureBean.setChildren(firstLevelStructureList);

        } else {
            let collect = structureBeans
                .filter(item => StringUtils.startsWith(item.getLevel(), level))
                .filter(item => {
                    let pointAfter = StringUtils.substringAfter(item.getLevel(), level);
                    return !StringUtils.contains(pointAfter, ".");
                });
            if (ArrayUtils.isNotEmpty(collect)) {
                structureBean.setChildren(collect);
            }
        }
    }
};

